// pages/SuSong/addcusLinkman/addcusLinkman.js
import request from '../../../utils/config.js';
import util from '../../../utils/util.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    custInfo: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  
  // 提交
  formSubmit: function(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    let tempData = e.detail.value;
    let custId = this.data.custInfo.custVo.custId;
    // console.log('this.data.custInfo详情==',this.data.custInfo)
    if (!tempData.linkman) {
      wx.showToast({ title: '联系人不能为空', icon: 'none', duration: 1000 });
      return
    }
    if (!tempData.tel ) {
      wx.showToast({ title: '手机号不能为空', icon: 'none', duration: 1000 });
      return
    }
    let sendData ={
      custName: this.data.custInfo.custVo.custName,
      linkman: tempData.linkman,
      tel: tempData.tel
    }
    request.requestMisApi('post', "custs/" + custId + "/customLinkman", sendData,(res)=> {
      console.log(res)
      wx.hideLoading();
      if (res.code == '00') {
        wx.showToast({
          title: res.msg,
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          wx.navigateBack();
        }, 1000);
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    }, (res) => {
      wx.hideLoading()
      wx.showToast({
        title:'新增联系人失败',
        icon: 'none',
        duration: 2000
      })
      console.log("新增联系人失败=", res)
    })

  },

  //点击收藏
  collect: function (e) {
    let info = this.data.custInfo;
    let _this = this;
    if (info.custVo.isFavor == "Y") {
      request.requestMisApi('delete', "favorCusts/" + info.custVo.custId, { custId: info.custVo.custId }, (res) => {
        console.log("取消收藏成功=", res);
        info.custVo.isFavor = "N";
        app.globalData.custInfo = info;
        _this.setData({
          custInfo: info
        });
      }, (res) => {
        wx.hideLoading()
        console.log("取消收藏失败=", res)
      });
    } else if (info.custVo.isFavor == "N") {
      request.requestMisApi('post', "favorCusts/" + info.custVo.custId, { custId: info.custVo.custId }, (res) => {
        console.log("收藏成功=", res);
        info.custVo.isFavor = "Y";
        app.globalData.custInfo = info;
        _this.setData({
          custInfo: info
        });
      }, (res) => {
        wx.hideLoading()
        console.log("收藏失败=", res)
      });
    }

  },
  //拨打电话
  callPhone: function (e) {
    let pho = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: pho
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.queryDetail();
  },

  // 查询页面详情
  queryDetail: function() {
    let info = app.globalData.custInfo;
    console.log(info)
    let startImgList = [
      { index: 1, slt: false },
      { index: 2, slt: false },
      { index: 3, slt: false }
    ];
    if (info.custVo.grade == 1) {
      startImgList = [
        { index: 1, slt: true },
        { index: 2, slt: false },
        { index: 3, slt: false }
      ]
    } else if (info.custVo.grade == 2) {
      startImgList = [
        { index: 1, slt: true },
        { index: 2, slt: true },
        { index: 3, slt: false }
      ]
    } if (info.custVo.grade == 3) {
      startImgList = [
        { index: 1, slt: true },
        { index: 2, slt: true },
        { index: 3, slt: true }
      ]
    }
    this.setData({
      startImgList: startImgList,
      grade: info.custVo.grade,
      custInfo: info,
    });
    if (info.custVo.grade == 3) {
      this.setData({
        sltA: true,
      });
    } else if (info.custVo.grade == 2) {
      this.setData({
        sltB: true,
      });
    } else if (info.custVo.grade == 1) {
      this.setData({
        sltC: true,
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // }
})